package com.u2b.business.user;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;


import com.u2b.beans.Activity;
import com.u2b.beans.Status;
import com.u2b.beans.User;
import com.u2b.business.location.DistanceService;
import com.u2b.business.net.FileUpload;

import com.u2b.business.time.TimeService;
import com.u2b.db.datasource.MyBeanUtils;
import com.u2b.db.user.UserDaoImpl;
import com.u2b.global.utils.Constants;
import com.u2b.global.utils.LoggerUtils;


public class UserService
{
    private final UserDaoImpl udp;
  
    private final DistanceService distanceProxy;
    private final TimeService timeProxy;
    private final FileUpload fileUpload;

    public UserService()
    {
        udp = new UserDaoImpl();
        distanceProxy = new DistanceService();
        timeProxy = new TimeService();
        fileUpload = new FileUpload();
        System.out.println("UserService的构造方法被调用了");

    }

    public int Register(HashMap<String, Object> initValues)
    {
        timeProxy.addCreateTime(initValues);
        int result = udp.RegisterUser(initValues);
        return result;
    }

    public HashMap<String, Object> login(HashMap<String, Object> initValues)
    {

      
        HashMap<String, Object> userHashMap = udp.login(initValues);
        userHashMap.put("distance", "0.00km");
        
        return userHashMap;
    }

    /*
     * 更新一次距离到数据库,三个参数:要查询的UID,经度,纬度.
     */
    public HashMap<String, Object> getUserByUid(HashMap<String, Object> initValues)
    {
        /*
         * 1.先得到查询的结果,但是这个uid是别人的uid，不是自己的uid！
         */
        int uid = Integer.valueOf(initValues.get("uid").toString());
        LoggerUtils.log("getUserByUid", uid+"", uid+"");
        HashMap<String, Object> userHashMap = udp.getUserByUid(uid);
        /*
         * 说明:先将自己的数据保存到数据库. (但是要获得准确数据,双方都必须是实时的,这才是问题的关键),但是用户一旦打开软件,就出现状态,
         * 所以在状态里面更新位置数据,并同时在这里面更新数据 
         * 2.将自己的数据更新到数据库
         */
      //  distanceProxy.updateDistanceToKVDB(initValues);
        /*
         * 3.将对方的位置信息从数据中提取出来并装载到hashMap中间.
         */
        distanceProxy.getDistanceFromKVDB(initValues,userHashMap);
//        User user = new User();
//        /*
//         * 4.封装成User对象发送出去
//         */
//        beanUtils.setPropertiesFromMap(userHashMap, user);
        return userHashMap;

    }

    public int settingMultiple(HashMap<String, Object> initValues, HttpServletRequest request)
    {
        String[] textParams = new String[]
        { "school", "user_name", "signature", "job", "hobby", "present", "uid" };
        HashMap<String, Object> userInfo = fileUpload.Upload(request, textParams, new String[]
        { ".jpg" }, new String[]
        { "head_URL" }, "user");
        int flag = udp.updateUser(userInfo);
        return flag;
    }

    public int setVideo(HashMap<String, Object> initValues, HttpServletRequest request)
    {
        String[] textParams = new String[]
        { "uid" };
        HashMap<String, Object> userVideo = fileUpload.Upload(request, textParams, new String[]
        {".jpg", ".3gp" }, new String[]
        { "photo_URL","video_URL" }, "user");
        int flag = udp.setVideo(userVideo);
        return flag;

    }

    public int setEmail(HashMap<String, Object> initValues)
    {
        return 1;
    }

    public int setPassword(HashMap<String, Object> initValues)

    {
        return 1;
    }

    /*
     * 将查询的结果按照时间排序.(并且要显示当前与以前发送的状态与距离的问题)主要是sql语句的问题.
     */
    public List<HashMap<String, Object>> getMyzones(HashMap<String, Object> initValues)
    {

        int uid = Integer.valueOf(initValues.get("uid").toString());
        List<HashMap<String, Object>> result = udp.getMyZones(uid);
       timeProxy.isExpire(result);
        distanceProxy.addDistance(result, initValues); // 只是单纯的添加距离
//        List<Object> beanList = new ArrayList<Object>();
//        for (HashMap<String, Object> map : result)
//        {
//            if (map.keySet().contains("aid"))
//            {
//                Activity bean = new Activity();
//                beanUtils.setPropertiesFromMap(map, bean);
//                beanList.add(bean);
//            }
//            else if (map.keySet().contains("sid"))
//            {
//                Status bean = new Status();
//                beanUtils.setPropertiesFromMap(map, bean);
//                beanList.add(bean);
//            }
//        }

        return result;
    }

}
